#include "stm32f10x.h"

int main() {}

void ADC1_Init() {
    ADC_InitTypeDef ADC_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;   // 独立模式
    ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;  // 单次转换模式
    ADC_InitStructure.ADC_ScanConvMode = DISABLE;  // 数模转换在单通道模式
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel = 1;
    ADC_InitStructure.ADC_ExternalTrigConv =
        ADC_ExternalTrigConv_None;  // 由软件而不是由外部触发
    ADC_Init(ADC1, &ADC_InitStructure);

    // 常规通道配置
    ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
    ADC_Cmd(ADC1, ENABLE); // 使能ADC1

    // 记得校准ADC
    // 启动ADC的校准
    ADC_StartCalibration(ADC1);
    // 等待校准完毕
    while(ADC_GetCalibrationStatus(ADC1));

}

uint16_t ADC1_Conv() {
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);

    // 等待准换完成
    while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
    return ADC_GetConversionValue(ADC1);
}